package com.itic.mobile.index;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.market.banner.persistence.model.Banner;
import com.itic.system.market.banner.persistence.model.BannerExample;
import com.itic.system.market.banner.service.BannerService;
import com.itic.system.market.category.persistence.model.Category;
import com.itic.system.market.category.persistence.model.CategoryExample;
import com.itic.system.market.category.service.CategoryService;

@Controller
@RequestMapping(value = "/mobile")
public class IndexMobile
     extends BaseMobile {
   @Autowired
   private BannerService bannerService;
   @Autowired
   private CategoryService categoryService;
   
	//商品过滤字段
   protected static String[] ignoreCategoryProperties = new String[] { "id","createUser", "isEffective", "updateTime", "note","createTime", "orgCode", "reorder","parentId" };
 	
	/**
	 * 登入
	 * @param request
	 * @param response
	 */ 
	@RequestMapping(value = "/index")   
	@ResponseBody 
	public Map<String, Object> in(HttpServletRequest request,HttpServletResponse response,String token,String device ){  
		 Map<String, Object> resultMap = new HashMap<String,Object>();
	     String errorCode = PARAM_ERROR_CODE;
	   	 String errorMsg = PARAM_ERROR_MSG;
		  try{  
			   Map<String, Object> dataMap = new HashMap<String,Object>();
			   if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(device) ){
				   SysUser user = sysUserService.getUserByTokenAndDevice(token, device);
				   dataMap.put("unReadMsg", user.getMsgTips());
			   }
			   
			   dataMap.put("banners", getBanners());
			   dataMap.put("categorys", getCategorys());
			   dataMap.put("products", getProductList(request,"",""));
			   resultMap.put(DATA, dataMap);
			   errorCode = SUCCESS_ERROR_CODE;
			   errorMsg = SUCCESS_ERROR_MSG;
          }catch(Exception e){  
        	  e.printStackTrace();
        	  errorCode = SERVER_ERROR_CODE;
		      errorMsg = SERVER_ERROR_MSG;
        	  logException("IndexMobile", "index", e.toString(),"首页",e);
          } 
          resultMap.put(CODE, errorCode);
		  resultMap.put(MSG, errorMsg);
		  isCrrossDomain(response);
		  return resultMap;
	}
	
	/**
	 * 获得轮播图信息
	 * @return
	 */
	public List<Map<String, Object>> getBanners(){
		BannerExample example = new BannerExample();
		com.itic.system.market.banner.persistence.model.BannerExample.Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		List<Banner> list = bannerService.list(example);
		return ValueHelper.tranList2MapList(list, ignoreProperties);
	}
	
	/**
	 * 获得类别信息
	 * @return
	 */
	public List<Map<String, Object>> getCategorys(){
		CategoryExample example = new CategoryExample();
		com.itic.system.market.category.persistence.model.CategoryExample.Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		List<Category> list = categoryService.list(example);
		return ValueHelper.tranList2MapList(list, ignoreCategoryProperties);
	}
	
	/**
	 * 获得类别信息
	 * @return
	 */
	public List<Map<String, Object>> getProducts(){
		CategoryExample example = new CategoryExample();
		com.itic.system.market.category.persistence.model.CategoryExample.Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		List<Category> list = categoryService.list(example);
		return ValueHelper.tranList2MapList(list, ignoreCategoryProperties);
	}
}